package com.china08.yxyapi.service.mg.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.mg.statistics.UserUploadResourceMonth;
import com.china08.yxyapi.entity.mg.statistics.UserUploadResourceTotal;
import com.china08.yxyapi.entity.mg.statistics.UserUploadResourceYear;
import com.china08.yxyapi.repository.mg.statistics.UserUploadResourceMonthRepository;
import com.china08.yxyapi.repository.mg.statistics.UserUploadResourceTotalRepository;
import com.china08.yxyapi.repository.mg.statistics.UserUploadResourceYearRepository;
import com.china08.yxyapi.service.mg.UserUploadResourceService;
import com.china08.yxyapi.util.DateUtils;

@Service
public class UserUploadResourceServiceImpl implements UserUploadResourceService {

    @Autowired
    private UserUploadResourceMonthRepository userUploadResourceMonthRepository;

    @Autowired
    private UserUploadResourceYearRepository userUploadResourceYearRepository;

    @Autowired
    private UserUploadResourceTotalRepository userUploadResourceTotalRepository;

    /**
     * 上传与分享的资源统计（年度）
     */
    @SuppressWarnings("null")
    @Override
    public void uploadResourceStatisticsForYear() {
        int startDate = Integer.parseInt(DateUtils.year(new Date()) + "00");
        int endDate = Integer.parseInt(DateUtils.year(new Date()) + "12");
        List<UserUploadResourceMonth> list = userUploadResourceMonthRepository.findBySynchAndIntMonthBetween(0,
                startDate, endDate);
        // uurm ：月统计数据对象
        for (UserUploadResourceMonth uurm : list) {
            // uury : 年统计数据对象
            UserUploadResourceYear uury;
            Integer yearDate = DateUtils.year(new Date());
            String userId = uurm.getUserId();
            String schoolId = uurm.getSchoolId();
            String id = userId + schoolId + yearDate;

            List<UserUploadResourceMonth> userLst = userUploadResourceMonthRepository
                    .findByUserIdAndSchoolIdAndIntMonthBetween(userId, schoolId, startDate, endDate);

            Long resCount = (long) 0, shareResCount = (long) 0, weikeCount = (long) 0, shareWeikeCount = (long) 0;
            for (UserUploadResourceMonth userInfo : userLst) {
                resCount += userInfo.getResCount();
                shareResCount += userInfo.getShareResCount();
                weikeCount += userInfo.getWeikeCount();
                shareWeikeCount += userInfo.getShareWeikeCount();
            }

            uury = userUploadResourceYearRepository.findOne(id);
            if (uury != null) {
                uury.setResCount(resCount);
                uury.setShareResCount(shareResCount);
                uury.setWeikeCount(weikeCount);
                uury.setShareWeikeCount(shareWeikeCount);
                uury.setSynch(0);
            } else {
                uury = new UserUploadResourceYear();
                uury.setId(id);
                uury.setUserId(userId);
                uury.setUserNick(uurm.getUserNick());
                uury.setIntYear(yearDate);
                uury.setStatisticsDate(new Date());
                uury.setSchoolId(schoolId);
                uury.setSchoolNick(uurm.getSchoolNick());
                uury.setResCount(resCount);
                uury.setShareResCount(shareResCount);
                uury.setWeikeCount(weikeCount);
                uury.setShareWeikeCount(shareWeikeCount);
                uury.setSynch(0);
            }
            // 将同步状态设置为 已同步
            uurm.setSynch(1);
            userUploadResourceMonthRepository.save(uurm);
            userUploadResourceYearRepository.save(uury);
        }
    }

    /**
     * 上传与分享的资源统计（总计）
     */
    @SuppressWarnings("null")
    @Override
    public void uploadResourceStatisticsForTotal() {
        List<UserUploadResourceYear> list = userUploadResourceYearRepository.findBySynch(0);
        // uury : 年统计数据对象
        for (UserUploadResourceYear uury : list) {
            // uurt : 总计数据对象
            UserUploadResourceTotal uurt;
            String userId = uury.getUserId();
            String schoolId = uury.getSchoolId();
            String id = userId + schoolId;

            List<UserUploadResourceYear> userLst = userUploadResourceYearRepository.findByUserIdAndSchoolId(userId,
                    schoolId);
            Long resCount = (long) 0, shareResCount = (long) 0, weikeCount = (long) 0, shareWeikeCount = (long) 0;
            for (UserUploadResourceYear userInfo : userLst) {
                resCount += userInfo.getResCount();
                shareResCount += userInfo.getShareResCount();
                weikeCount += userInfo.getWeikeCount();
                shareWeikeCount += userInfo.getShareWeikeCount();
            }

            uurt = userUploadResourceTotalRepository.findOne(id);
            if (uurt != null) {
                uurt.setResCount(resCount);
                uurt.setShareResCount(shareResCount);
                uurt.setWeikeCount(weikeCount);
                uurt.setShareWeikeCount(shareWeikeCount);
            } else {
                uurt = new UserUploadResourceTotal();
                uurt.setId(id);
                uurt.setUserId(userId);
                uurt.setUserNick(uury.getUserNick());
                uurt.setStatisticsDate(new Date());
                uurt.setSchoolId(schoolId);
                uurt.setSchoolNick(uury.getSchoolNick());
                uurt.setResCount(resCount);
                uurt.setShareResCount(shareResCount);
                uurt.setWeikeCount(weikeCount);
                uurt.setShareWeikeCount(shareWeikeCount);
            }
            // 将同步状态设置为 已同步
            uury.setSynch(1);
            userUploadResourceYearRepository.save(uury);
            userUploadResourceTotalRepository.save(uurt);
        }
    }

}
